package com.oritsh.hl7.his.utils;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v26.message.ORM_O01;
import ca.uhn.hl7v2.model.v26.segment.*;
import com.oritsh.hl7.his.model.Patient;
import com.oritsh.hl7.his.model.TechApply;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: zarra
 * Date: 14-3-15
 * Time: 下午4:55
 * Copyright Shanghai Orient Rain Information Technology Co.,Ltd.
 */
public class Convert {
    public  PID convertPID(PID pid,Patient patient) throws DataTypeException {
        //病人编号
        pid.getPid2_PatientID().getIDNumber().setValue(patient.getPid());
        pid.getPid3_PatientIdentifierList(0).getCx1_IDNumber().setValue(patient.getPid());
        //病人姓名
        pid.getPatientName(0).getGivenName().setValue(patient.getGivenName());
        pid.getPatientName(0).getFamilyName().getFn1_Surname().setValue(patient.getFamilyName());
        //pid.getPatientAccountNumber().getID().setValue("77001672");
        //病人性别
        pid.getPid8_AdministrativeSex().setValue(patient.getSex());

        pid.getPid7_DateTimeOfBirth().setValue(patient.getDateTimeOfBirth());
        return pid;
    }

    public PV1 convertPV1(PV1 pv1,Patient patient) throws DataTypeException {
        //pv1.getPv12_PatientClass().setValue("pv12");
        //pv1.getPv18_ReferringDoctor(0).getIDNumber().setValue("pv18_id");
        //pv1.getPv18_ReferringDoctor(0).getFamilyName().getFn1_Surname().setValue("pv18_family");
        pv1.getPv115_AmbulatoryStatus(0).setValue("B6");
        pv1.getPv119_VisitNumber().getCx1_IDNumber().setValue(patient.getVisitNumber());
        return pv1;
    }

    public OBR convertTechApply(OBR obr,TechApply techApply) throws DataTypeException {
        obr.getObr2_PlacerOrderNumber().getEntityIdentifier().setValue("1111");
        obr.getObr4_UniversalServiceIdentifier().getCwe1_Identifier().setValue("SSSS");
        obr.getObr4_UniversalServiceIdentifier().getCwe2_Text().setValue(techApply.getModality());
        obr.getObr4_UniversalServiceIdentifier().getCwe3_NameOfCodingSystem().setValue("obr4_3");
        obr.getObr4_UniversalServiceIdentifier().getCwe4_AlternateIdentifier().setValue("OBR4_4");
        obr.getObr4_UniversalServiceIdentifier().getCwe5_AlternateText().setValue("OBR4_5");
        obr.getObr13_RelevantClinicalInformation().setValue("OBR13");

        obr.getObr16_OrderingProvider(0).getIDNumber().setValue("0000");
        obr.getObr16_OrderingProvider(0).getFamilyName().getFn1_Surname().setValue("System");
        obr.getObr16_OrderingProvider(0).getGivenName().setValue("Admin");

        obr.getObr18_PlacerField1().setValue(techApply.getAccessNumber());//accessNumber
        obr.getObr19_PlacerField2().setValue(techApply.getBodyPartCode());//bodyPartCode
        obr.getObr20_FillerField1().setValue("OBR20");
        obr.getObr24_DiagnosticServSectID().setValue(techApply.getModality());
        obr.getObr12_DangerCode().getCwe1_Identifier().setValue("121212");
        obr.getObr30_TransportationMode().setValue("obr30");
        obr.getObr34_Technician(0).getNdl1_Name().getIDNumber().setValue("obr34");
        obr.getObr34_Technician(0).getNdl1_Name().getGivenName().setValue("OBR34");

        obr.getObr44_ProcedureCode().getCne1_Identifier().setValue("441");
        obr.getObr44_ProcedureCode().getCne2_Text().setValue("442");
        return obr;
    }

    public ORM_O01 convetTechApply(TechApply techApply) throws IOException, HL7Exception {
        return convetTechApply(techApply,false);
    }

    public ORM_O01 convetTechApply(TechApply techApply,boolean isDelete) throws IOException, HL7Exception {
        ORM_O01 orm = new ORM_O01();
        orm.initQuickstart("ORM", "O01", "P");

        Patient patient = techApply.getPatient();

        MSH mshSegment = orm.getMSH();
        mshSegment.getSendingApplication().getNamespaceID().setValue("ORITSH");
        mshSegment.getSequenceNumber().setValue("123");

        PID pid =  orm.getPATIENT().getPID();
        convertPID(pid,patient);

        PV1 pv1 = orm.getPATIENT().getPATIENT_VISIT().getPV1();
        convertPV1(pv1,patient);

        ORC orc = orm.getORDER().getORC();
        //医嘱操作
        if (isDelete)
            orc.getOrderControl().setValue("CA");//NW or CA
        else
            orc.getOrderControl().setValue("NW");//NW or CA
        //医嘱编号
        orc.getPlacerOrderNumber().getEi1_EntityIdentifier().setValue("11110000");
        orc.getDateTimeOfTransaction().setValue("20100101232323");
        //下嘱医师
//        orc.getEnteredBy()..setValue("77001348");
//        orc.getEnteredBy().getFamilyName().setValue("Jia");
//        orc.getEnteredBy().getGivenName().setValue("Lei");
        orc.getOrc12_OrderingProvider(0).getFamilyName().getFn1_Surname().setValue("system");
        orc.getOrc12_OrderingProvider(0).getIDNumber().setValue("0000");

        OBR obr = orm.getORDER().getORDER_DETAIL().getOBR();

        convertTechApply(obr,techApply);

        return orm;
    }
}
